<?php


namespace logicmodel;


use comservice\Response;
use datamodel\Bill;
use datamodel\Users;
use think\Db;

class AccountLogic
{
    private $billData;
    private $usersData;
    public function __construct()
    {
        $this->billData = new Bill();
        $this->usersData = new Users();
    }

    /**
     * 增加账户余额
     * @param $uid
     * @param $currency_id
     * @param $account
     * @param $bill_type
     * @param $remark
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function  addAccount($uid,$currency_id,$account,$bill_type,$remark){
        if($currency_id == 1){
            $before_account =   $this->usersData->where(['id'=>$uid])->value('account');
            $after_account = bcadd($account,$before_account,10); //变动后
            Db::startTrans();
            $result =  $this->usersData->where(['id'=>$uid])->update(['account'=>$after_account]);
            if($result <= 0){
                Db::rollback();
                return false;
            }
            $result =  $this->bill($uid,$currency_id,$account,$before_account,$after_account,$bill_type,$remark,1);
            if($result > 0){
                Db::commit();
                return true;
            }
            Db::rollback();
            return false;
        }elseif ($currency_id == 2){
                $before_account =   $this->usersData->where(['id'=>$uid])->value('integral');
                $after_account = bcadd($account,$before_account,10); //变动后
                Db::startTrans();
                $result =  $this->usersData->where(['id'=>$uid])->update(['integral'=>$after_account]);
                if($result <= 0){
                    Db::rollback();
                    return false;
                }
                $result =  $this->bill($uid,$currency_id,$account,$before_account,$after_account,$bill_type,$remark,1);
                if($result > 0){
                    Db::commit();
                    return true;
                }
                Db::rollback();
                return false;
        }
        return false;
    }


    /**
     * 扣除系统账户
     * @param $uid
     * @param $currency_id
     * @param $account
     * @param $bill_type
     * @param $remark
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    /**
     * 扣除系统账户
     * @param $uid
     * @param $currency_id
     * @param $account
     * @param $bill_type
     * @param $remark
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function  subAccount($uid,$currency_id,$account,$bill_type,$remark){
        if($currency_id == 1){
            $before_account =   $this->usersData->where(['id'=>$uid])->value('account');
            if($before_account < $account) return false;
            $after_account = bcsub($before_account,$account,10);  //变动后
            Db::startTrans();
            $result =  $this->usersData->where(['id'=>$uid])->update(['account'=>$after_account]);
            if($result <= 0){
                Db::rollback();
                return false;
            }
            $result = $this->bill($uid,$currency_id,$account,$before_account,$after_account,$bill_type,$remark,2);
            if($result > 0){
                Db::commit();
                return true;
            }
            return false;
        } elseif($currency_id == 2){
            $before_account =   $this->usersData->where(['id'=>$uid])->value('integral');
            if($before_account < $account) return false;
            $after_account = bcsub($before_account,$account,10);  //变动后
            Db::startTrans();
            $result =  $this->usersData->where(['id'=>$uid])->update(['integral'=>$after_account]);
            if($result <= 0){
                Db::rollback();
                return false;
            }
            $result = $this->bill($uid,$currency_id,$account,$before_account,$after_account,$bill_type,$remark,2);
            if($result > 0){
                Db::commit();
                return true;
            }
            return false;
        }
        return false;
    }


    /**
     * 生成系统流水
     * @param $uid
     * @param $currency_id
     * @param $account
     * @param $before_account
     * @param $after_account
     * @param $bill_type
     * @param $remark
     * @param $type
     * @return bool
     */
    private function bill($uid,$currency_id,$account,$before_account,$after_account,$bill_type,$remark,$type){
        $data['uid'] = $uid;
        $data['currency_id'] = $currency_id;
        $data['account'] = $account;
        $data['before_account'] = $before_account;
        $data['after_account'] = $after_account;
        $data['bill_type'] = $bill_type;
        $data['remark'] = $remark;
        $data['type'] = $type;
        $data['create_time'] = date('Y-m-d H:i:s');
        $id = $this->billData->saveEntityAndGetId($data);
        if($id > 0) return true;
        return false;
    }

    /**
     * 账户余额
     * @param $userInfo
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userAccount($userInfo){
        $data = ['account'=>$userInfo['account'],'integral'=>$userInfo['integral']];
        return  Response::success('success',$data);
    }


}